<script lang="ts" setup>
import { ref, provide, shallowRef } from 'vue';
import DialogVue from '../components/Dialog.vue';
import MessagePropertyVue from './Connection/MessageProperty.vue';
import { Message, MessageValueToEdit, ReceiveAction } from '../common/types';
import { DialogResult, SecsFormat } from '../common/enums';
import ValuePropertyVue from './Connection/ValueProperty.vue';
import ReceiveActionPropertyVue from './Connection/ReceiveActionProperty.vue';
// import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';

const receiveActionDialog = shallowRef();
const receiveAction = shallowRef();
const receiveActionDialogApi = {
    data: ref<ReceiveAction>({
        matchRules: [],
        replySecondary: true,
        perforAction: []
    }),
    open(title?: string) {
        return receiveActionDialog.value.open(title ?? 'Property');
    },
    close(dialogResult: DialogResult) {
        receiveActionDialog.value.close(dialogResult);
    }
}
provide('receiveActionDialogApi', receiveActionDialogApi);

const messagePropertyDialog = shallowRef();
const messageProperty = shallowRef();
const messagePropertyDialogApi = {
    data: ref<Message>({
        name: '',
        desc: '',
        head: { s: 0, f: 0, systemByte: null, isExpectReply: false },
        body: null
    }),
    open(title?: string) {
        return messagePropertyDialog.value.open(title ?? 'Property');
    },
    close(dialogResult: DialogResult) {
        messagePropertyDialog.value.close(dialogResult);
    }
}
provide('messagePropertyDialogApi', messagePropertyDialogApi);


const valuePropertyDialog = shallowRef();
const valueProperty = shallowRef();
const valuePropertyDialogApi = {
    beforeClosing: null as any,
    data: ref<MessageValueToEdit>({
        name: '',
        desc: '',
        format: SecsFormat.Unknow,
        mappingRef: null,
        value: null,
        valueString: null,
    }),
    beforeDialogClosing(arg: any) {
        if (valuePropertyDialogApi.beforeClosing) {
            arg.isPrevent = valuePropertyDialogApi.beforeClosing(arg.dialogResult);
        }
        return false;
    },
    open(title: string | null, beforeClosing?: (dialogResult: DialogResult) => boolean) {
        valuePropertyDialogApi.beforeClosing = beforeClosing;
        return valuePropertyDialog.value.open(title ?? 'Property');
    },
    close(dialogResult: DialogResult) {
        if (valuePropertyDialog.value.close(dialogResult) === false) {
            return;
        }
        valuePropertyDialogApi.data.value.valueString = null;
    }
}
provide('valuePropertyDialogApi', valuePropertyDialogApi);
</script>
<template>
    <slot name="body"></slot>
    <DialogVue ref="receiveActionDialog" :width="'600'" :height="'400'">
        <ReceiveActionPropertyVue ref="receiveAction" :data="receiveActionDialogApi.data.value"
            @close="receiveActionDialogApi.close">
        </ReceiveActionPropertyVue>
    </DialogVue>
    <DialogVue ref="messagePropertyDialog" :width="'400'" :height="'400'" :is-show-mask="true">
        <MessagePropertyVue ref="messageProperty" :data="messagePropertyDialogApi.data.value"
            @close="messagePropertyDialogApi.close">
        </MessagePropertyVue>
    </DialogVue>
    <DialogVue ref="valuePropertyDialog" :width="'400'" :height="'400'"
        @before-closing="valuePropertyDialogApi.beforeDialogClosing" :is-show-mask="true">
        <ValuePropertyVue ref="valueProperty" :data="valuePropertyDialogApi.data.value"
            @close="valuePropertyDialogApi.close">
        </ValuePropertyVue>
    </DialogVue>
</template>